package com.ykg.service.impl;

import com.ykg.dto.*;
import com.ykg.mapper.*;
import com.ykg.pojo.*;
import com.ykg.service.IDTOService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * TODO
 * 类(接口)：该类主要是创建需要返回的数据表单
 * Created on 21:06 2021/6/23.
 *
 * @Author: laobian
 * @Description:
 */
@Service
public class DTOServiceImpl implements IDTOService {
    @Autowired
    private OwnerMapper ownerMapper;

    @Autowired
    private ShipMapper shipMapper;

    @Autowired
    private StampMapper stampMapper;

    @Autowired
    private ListMapper listMapper;

    @Autowired
    private BeltMapper beltMapper;

    @Autowired
    private CabinMapper cabinMapper;


    @Override
    /**
    * TODO
    *  创建卸货清单，未完成时无卸货信息；完成后则由卸货信息
    * @params：
      @param listCharge
    * @return {@link ListDTO}
    * @Author: laobian
    * @Date: 21:21 2021/6/23
    */
    public ListDTO createListDto(ListCharge listCharge) {
        ListDTO listDTO = new ListDTO();
        BeanUtils.copyProperties(listCharge,listDTO);

        Owner owner = ownerMapper.findOwnerById(listCharge.getOwnerId());
        listDTO.setOwnerName(owner.getName());
        listDTO.setOwnerTel(owner.getContentway());

        Ship ship = shipMapper.findShipById(listCharge.getShipId());
        listDTO.setShipName(ship.getShipName());
        listDTO.setBerth(ship.getBerth());
        listDTO.setHostName(ship.getHostName());
        listDTO.setHostTel(ship.getHostTel());

        if(listCharge.getState()==4){
            List<UnloadInfo> unloadInfoList = stampMapper.findUnloadByListId(listCharge.getListId());
            listDTO.setUnLoadDTOList(unloadInfoList);
        }
        return listDTO;
    }

    /**
     *
     *  生成通知单dto
     * @params：
     @param noticeLetter
      * @return {@link NoticeDTO}
     * @Author: laobian
     * @Date: 13:51 2021/6/28
     */
    public NoticeDTO createNoticeDto(NoticeLetter noticeLetter){

        NoticeDTO noticeDTO = new NoticeDTO();
        BeanUtils.copyProperties(noticeLetter,noticeDTO);

        Owner owner = ownerMapper.findOwnerById(noticeLetter.getOwnerId());
        noticeDTO.setOwnerId(owner.getOwnerId());
        noticeDTO.setOwnerName(owner.getName());
        noticeDTO.setOwnerTel(owner.getContentway());

        Ship ship = shipMapper.findShipById(noticeLetter.getShipId());
        noticeDTO.setShipId(ship.getShipId());
        noticeDTO.setShipName(ship.getShipName());
        noticeDTO.setBerth(ship.getBerth());
        noticeDTO.setHostName(ship.getHostName());
        noticeDTO.setHostTel(ship.getHostTel());

        ListCharge listCharge = listMapper.findListById(noticeLetter.getListId());
        noticeDTO.setListId(noticeLetter.getListId());
        noticeDTO.setQuantity(listCharge.getQuantity());
        return  noticeDTO;
    }

    @Override
    /**
     * TODO
     *  生成合同dto
     * @params：
     @param contract
      * @return {@link ContractDTO}
     * @Author: laobian
     * @Date: 13:51 2021/6/28
     */
    public ContractDTO createContractDto(Contract contract) {

        ContractDTO contractDTO =  new ContractDTO();
        BeanUtils.copyProperties(contract,contractDTO);

        Owner owner = ownerMapper.findOwnerById(contract.getOwnerId());
        contractDTO.setOwnerId(contract.getOwnerId());
        contractDTO.setContentway(owner.getContentway());
        contractDTO.setOwnerName(owner.getName());
        return contractDTO;
    }

    @Override
    /**
     * TODO
     *  生成委托单dto
     * @params：
     @param entrust
      * @return {@link EntrustDTO}
     * @Author: laobian
     * @Date: 13:52 2021/6/28
     */

    public EntrustDTO createEntrustDTO(Entrust entrust){

        EntrustDTO entrustDTO = new EntrustDTO();
        BeanUtils.copyProperties(entrust,entrustDTO);

        /*
        货主相关
         */
        Owner owner = ownerMapper.findOwnerById(entrust.getOwnerId());
        entrustDTO.setOwnerId(owner.getOwnerId());
        entrustDTO.setOwnerName(owner.getName());
        entrustDTO.setOwnerTel(owner.getContentway());
        /*
        船相关
         */
        Ship ship = shipMapper.findShipById(entrust.getShipId());
        entrustDTO.setShipId(ship.getShipId());
        entrustDTO.setShipName(ship.getShipName());
        entrustDTO.setBerth(ship.getBerth());
        entrustDTO.setManName(ship.getHostName());
        entrustDTO.setManTel(ship.getHostTel());
        /*
        卸货清单相关
         */
        ListCharge listCharge = listMapper.findListById(entrust.getListId());
        entrustDTO.setListId(listCharge.getListId());
        entrustDTO.setQuantity(listCharge.getQuantity());
        /*
        卸货信息相关
         */
        List<UnloadInfo> unloadInfoList = stampMapper.findUnloadByListId(listCharge.getListId());
        entrustDTO.setUnLoadDTOList(unloadInfoList);
        return  entrustDTO;
    }
    @Override
    public RunStampDTO createRunStampDTO(EntrustDTO entrustDTO){
        RunStampDTO runStampDTO = new RunStampDTO();
        BeanUtils.copyProperties(entrustDTO,runStampDTO);

        runStampDTO.setUnloadInfos(stampMapper.findUnloadByListId(runStampDTO.getListId()));
        return runStampDTO;
    }
    @Override
    public StowBeltDTO createStowBeltDTO(Entrust entrust){
        StowBeltDTO stowBeltDTO = new StowBeltDTO();
        BeanUtils.copyProperties(entrust,stowBeltDTO);

        Owner owner = ownerMapper.findOwnerById(entrust.getOwnerId());
        stowBeltDTO.setOwnerId(owner.getOwnerId());
        stowBeltDTO.setOwnerName(owner.getName());
        stowBeltDTO.setOwnerTel(owner.getContentway());

        Ship ship = shipMapper.findShipById(entrust.getShipId());
        stowBeltDTO.setShipId(ship.getShipId());
        stowBeltDTO.setShipName(ship.getShipName());
        stowBeltDTO.setBerth(ship.getBerth());
        stowBeltDTO.setGoodsType("A类货");


        ListCharge listCharge = listMapper.findListById(entrust.getListId());
        stowBeltDTO.setQuantity(listCharge.getQuantity());
        stowBeltDTO.setUnloadInfos(stampMapper.findUnloadByListId(stowBeltDTO.getListId()));
        List<Belt> belts = beltMapper.findAll();
        int busyBelts = 0;
        int freeBelts = 0;
        for(int i = 0;i<8;i++){
            if(belts.get(i).getState() == 1){
                busyBelts++;
            }else {
                freeBelts++;
            }
        }
        stowBeltDTO.setBusyBelts(busyBelts);
        stowBeltDTO.setFreeBelts(freeBelts);
        stowBeltDTO.setStowState(0);
        List<Belt> belts1 = beltMapper.findByEntrustId(entrust.getEntrustId());
        stowBeltDTO.setBelts(belts1);
        return stowBeltDTO;
    }

    @Override//卸货详细信息
    public UnloadShipDTO createUnloadShipDTO(Entrust entrust){
        UnloadShipDTO unloadShipDTO = new UnloadShipDTO();
        BeanUtils.copyProperties(entrust,unloadShipDTO);

        Owner owner = ownerMapper.findOwnerById(entrust.getOwnerId());
        unloadShipDTO.setOwnerId(owner.getOwnerId());
        unloadShipDTO.setOwnerName(owner.getName());

        Ship ship = shipMapper.findShipById(entrust.getShipId());
        unloadShipDTO.setShipId(ship.getShipId());
        unloadShipDTO.setShipName(ship.getShipName());
        unloadShipDTO.setBerth(ship.getBerth());

        ListCharge listCharge = listMapper.findListById(entrust.getListId());
        unloadShipDTO.setListId(listCharge.getListId());
        unloadShipDTO.setQuantity(listCharge.getQuantity());

        unloadShipDTO.setUnloadInfos(stampMapper.findUnloadByListId(unloadShipDTO.getListId()));
        List<Cabin> cabins = cabinMapper.findByShipId(unloadShipDTO.getShipId());
        String listId = unloadShipDTO.getListId();
        for(int i = 0;i<cabins.size();i++){
            cabins.get(i).setListId(listId);
        }
        unloadShipDTO.setCabins(cabins);

        return unloadShipDTO;
    }


}
